<?php
// +----------------------------------------------------------------------
// | EtrolPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Author: Author: LJQ <245369209@qq.com> 
// +----------------------------------------------------------------------

namespace app\basic\controller\api;
use app\common\controller\Api;
use think\response\Json;


/**
 * 救援团队管理
 * @package app\Rescue
 * @author LJQ<245369209@qq.com>
 */
class Rescue extends Api

{
	protected $wfType = 9;

    /**
   *  救援团队列表
   * @param string 
   * @return Json
   */

	public function rescueList(){
            $where['categoryId'] = ['eq',1] ;
		//分页参数
		if(($limit = input('limit')) && ($page = input('page'))){
			$startLimit =  ($page-1)*$limit;
			// 救援团队列表
			$info =collection(model('Rescue')->where($where)->limit($startLimit.','.$limit)->select())->toArray();
		}else{
			// 救援团队列表
			$info =collection(model('Rescue')->where($where)->select())->toArray();
		}
	   	 
	 	//总数
	   	 $count = model('Rescue')->where($where)->count('id'); 
		
    		if ($count == 0) {
         			return json(['code'=>1,'msg'=>'没有找到救援团队','count'=>$count,'data'=>$info]);
       		} 
       	
       		if ($count > 0){
       			return json(['code'=>1,'msg'=>'救援团队信息','count'=>$count,'data'=>$info]);
       		}
            		return json(['code'=>0,'msg'=>'数据异常']);
   	 	 }
   	  
   	      //添加救援团队
   	 public function add($data){
          
          if (empty($data)) {
              return json(['code'=>0,'msg'=>'数据异常,请检查']);
          }
          $model = model('Basic/Rescue');

          $result = $model->change($data);
          if (false != $result) {
            setlog('Rescueadd','Rescue',session('user_auth.id'));
              return json(['code'=>1,'msg'=>'救援团队添加成功']);
          } else {
              return json(['code'=>0,'msg'=>'救援团队添加失败,请检查']);
          }
      }

   	

   /**
   * 救援团队修改
   * @param  string RescueId 救援团队id,
   * @return Json
   */
   	public function edit($data,$rescueId){
        
          if (empty($rescueId)) {
              return json(['code'=>0,'msg'=>'救援团队不存在,请检查']);
          }
          $model = model('Basic/Rescue');     
          if ($model->change($data)) {
            setlog('Rescueupdate','Rescue',session('user_auth.id'));
             return json(['code'=>1,'msg'=>'救援团队信息修改成功']);
          }else{
             return json(['code'=>0,'msg'=>'救援团队信息修改失败,请检查']);
          }
    }
    /**
   *  删除救援团队
   * @param string RescueId 救援团队id,
   * @param Json
   */
     
    public function del($rescueId) {
        $model = model('Basic/Rescue');
      	if (! $rescueId ||empty(($model->where(array('id'=>$rescueId))->find()))) {
           	return  json(['code'=>0,'msg'=>'救援团队id不存在,请检查']);
        }
        if($model->destroy(['id'=>$rescueId]) !== FALSE){
          setlog('Rescuedel','Rescue',session('user_auth.id'));
        	 return json(['code'=>1,'msg'=>'救援团队删除成功']);
       }else{
         	 return json(['code'=>0,'msg'=>'救援团队删除失败,请检查']);
       }
    }

     
   /**
   *   查询救援团队
   * @param keyword救援团队名称
   * @return Json
   */
   	public function search($keyword=null){
   		$where ['categoryId'] = ['eq',1] ;

		if($keyword){
	
			$where['fullName'] = ['like','%'.$keyword.'%'];
		}
   		//分页参数
		if(($limit = input('limit')) && ($page = input('page'))){
			$startLimit =  ($page-1)*$limit;
			//救援团队列表
			$info =collection(model('Rescue')->where($where)->limit($startLimit.','.$limit)->select())->toArray();
		}else{
			//救援团队列表
			$info =collection(model('Rescue')->where($where)->select())->toArray();
		}
	 	//总数
	       $count = model('Rescue')->where($where)->count('id'); 
		 
    	       if (empty($info) && $count == 0) {
              	return json(['code'=>0,'msg'=>'没有找到救援团队','count'=>$count]);
       	} 
       	
       	if ($count > 0){
       		return json(['code'=>1,'msg'=>'物资救援团队','count'=>$count,'data'=>$info]);
       	}
      }

	/**
	 * 获取救援团队位置
	 * @param string $enterpriseId
	 * @return Json
	 */
	public function getRescuePosition($rescueId){
		$position = model('Rescue')->where('id',$rescueId)->value('latitudeLongitude');
		if($position){
			return json(['code'=>1,'msg'=>'','data'=>$position]);
		}else{
			return json(['code'=>0,'msg'=>'没有对应位置']);
		}
	}
      
      /**
       * 申请审核
       * @param string $rescueId
       */
      public function audit($rescueId = null){
      	$workflow = model('Workflow')->where('wfType',$this->wfType)->find();
      	if(empty($workflow)||empty($workflow->steps)){
      		return json(['code'=>0,'msg'=>'无对应审核流程','count'=>'','data'=>'']);
      	}else{
	      	$rescue = model('Rescue')->get($rescueId);
	      	if($rescue->isUpdate(true)->save(['checkStatus'=>2]) !== false){
	      		$data['title'] = $rescue['fullName'];
	      		$data['wfFlowInfoId'] = $workflow['id'];
	      		$data['wfType'] = $this->wfType;
	      		$data['busiId'] = $rescueId;
	      		$data['applyPerson'] = session('user_auth.id');
	      		if(model('Wfitem')->save($data)){
	      			return json(['code'=>1,'msg'=>'申请成功','count'=>'','data'=>'']);
	      		}else{
	      			return json(['code'=>0,'msg'=>'申请失败','count'=>'','data'=>'']);
	      		}
	      	}
      	}
      	return json(['code'=>0,'msg'=>'申请失败','count'=>'','data'=>'']);
      	 
      }
}